<?php
require("db_params.php");

include_once('CAS-UC/CAS.php');

// Uncomment to enable debugging
phpCAS::setDebug();

// Inicializa librería phpCAS
phpCAS::client(CAS_VERSION_2_0, 'sso.uc.cl', 443, '/cas');
phpCAS::setNoCasServerValidation();
//exije estar Logueado uc
phpCAS::forceAuthentication();

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr); 
return $xmlStr; 
} 


if(isset($_GET['radio']))
{
	$radio = $_GET['radio'];
	
}
else
{
	
	$radio='100';
	}
	
if (!isset($_SESSION['username'])) {
   $_SESSION['username'] = phpCAS::getUser();//this stores their network user id
}
else
    {
    phpCAS::forceAuthentication();
    }
	
	$username=$_SESSION['username'];
	
	
	$res = @mysql_query("select lat from direccion_usuarios where user_name='$username'");
												$row = mysql_fetch_array($res);
                                                    $center_lat=$row['lat'];
													$res = @mysql_query("select lng from direccion_usuarios where user_name='$username'");
													$row = mysql_fetch_array($res);
                                                    $center_lng=$row['lng'];

	
// Select all the rows in the markers table
$query = "SELECT * FROM usuarios as usuarios, direccion_usuarios as direccion_usuarios where usuarios.user_name=direccion_usuarios.user_name and (SELECT (acos(sin(radians('$center_lat')) * sin(radians(lat)) + cos(radians('$center_lat')) * cos(radians(lat)) * cos(radians('$center_lng')- radians(lng))) * 6378)<'$radio')";
$result = mysql_query($query);


header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<locations>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<location ';
  echo 'name="' . parseToXML($row['nombre']) . '" ';
  echo 'surename="' . parseToXML($row['apellido1']) . '" ';
  echo 'uid_facebook="' . parseToXML($row['uid_facebook']) . '" ';
  echo 'address="' . parseToXML($row['email']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['tiene_auto'] . '" ';
  if($row['uid_facebook']!="''"){
	echo 'tiene_fb="1"';
	}
	else{
		echo 'tiene_fb="0"';
		}
  echo '/>';
}

// End XML file
echo '</locations>';

?>